package com.xiaotao.service.impl;

import com.xiaotao.dto.TeamApplyDTO;
import com.xiaotao.dto.TeamDetailDTO;
import com.xiaotao.dto.TeamMemberDTO;
import com.xiaotao.entity.TeamMember;
import com.xiaotao.entity.TeamPost;
import com.xiaotao.mapper.TeamMemberMapper;
import com.xiaotao.mapper.TeamPostMapper;
import com.xiaotao.service.TeamService;
import com.xiaotao.service.TeamPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeamServiceImpl implements TeamService {

    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Autowired
    private TeamPostMapper teamPostMapper;

    @Autowired
    private TeamPostService teamPostService;

    @Override
    @Transactional
    public boolean applyToTeam(Integer userId, TeamApplyDTO applyDTO) {
        TeamMember existing = teamMemberMapper.selectByPostIdAndUserId(applyDTO.getPostId(), userId);
        if (existing != null) {
            return false;
        }

        TeamMember application = new TeamMember();
        application.setPostId(applyDTO.getPostId());
        application.setUserId(userId);
        application.setStatus("申请中");
        application.setJoinTime(LocalDateTime.now());

        return teamMemberMapper.insert(application) > 0;
    }

    @Override
    @Transactional
    public boolean approveApplication(Integer ownerId, Long applicationId) {
        TeamMember application = teamMemberMapper.selectById(applicationId);
        if (application == null) {
            return false;
        }

        TeamPost post = teamPostMapper.selectById(application.getPostId());
        if (post == null || !post.getUserId().equals(ownerId)) {
            return false;
        }

        application.setStatus("已加入");
        return teamMemberMapper.updateStatus(application) > 0;
    }

    @Override
    @Transactional
    public boolean rejectApplication(Integer ownerId, Long applicationId) {
        TeamMember application = teamMemberMapper.selectById(applicationId);
        if (application == null) {
            return false;
        }

        TeamPost post = teamPostMapper.selectById(application.getPostId());
        if (post == null || !post.getUserId().equals(ownerId)) {
            return false;
        }

        application.setStatus("已拒绝");
        return teamMemberMapper.updateStatus(application) > 0;
    }

    @Override
    public TeamDetailDTO getTeamDetail(Long postId, Integer currentUserId) {
        TeamPost post = teamPostMapper.selectById(postId);
        if (post == null) {
            return null;
        }

        TeamDetailDTO detail = new TeamDetailDTO();
        detail.setPost(post);

        if (currentUserId != null) {
            detail.setIsLiked(teamPostService.checkLikeStatus(currentUserId, postId));
        } else {
            detail.setIsLiked(false);
        }

        List<TeamMember> members = teamMemberMapper.selectByPostId(postId);
        List<TeamMemberDTO> memberDTOs = members.stream().map(member -> {
            TeamMemberDTO dto = new TeamMemberDTO();
            dto.setUserId(member.getUserId());
            dto.setRole(member.getRole());
            dto.setStatus(member.getStatus());
            dto.setNickname(member.getNickname() != null ? member.getNickname() : "用户" + member.getUserId());
            dto.setAvatar(member.getAvatar() != null ? member.getAvatar() : "default_avatar.jpg");
            return dto;
        }).collect(Collectors.toList());

        detail.setMembers(memberDTOs);
        return detail;
    }

    @Override
    public List<TeamMember> getTeamMembers(Long postId) {
        return teamMemberMapper.selectByPostId(postId);
    }

    @Override
    public boolean checkLikeStatus(Integer userId, Long postId) {
        if (userId == null || postId == null) {
            return false;
        }
        return teamPostService.checkLikeStatus(userId, postId);
    }
}